package com.ecarx.camera.worker.car.aosp;

import android.car.hardware.CarPropertyValue;

import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.worker.car.FunctionStatus;
import com.ecarx.camera.worker.car.ICarSensorProxy;
import com.ecarx.camera.worker.car.adaptapi.AdaptWrapperHelper;
import com.ecarx.camera.worker.car.constant.IllegalData;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 车身传感器可用状态、取值或监听的接口的Aosp实现
 */
public class CarSensorProxyAospImpl implements ICarSensorProxy {

    private static final String TAG = "CarSensorProxyAospImpl";

    /**
     * 记录ISensorListener与PropertyEventCallbackWrapper映射关系
     */
    private final ConcurrentHashMap<Integer, PropertyEventCallbackWrapper> mEventCallbackWrapperMap = new ConcurrentHashMap<>();

    @Override
    public boolean isSensorSupported(int sensorId) {
        int propId = AdaptWrapperHelper.sensorIdToPropertyId(sensorId);
        return AospPropertyManagerHelper.isCarPropertySupported(propId);
    }

    @Override
    public boolean isSensorActive(int sensorId) {
        int propId = AdaptWrapperHelper.sensorIdToPropertyId(sensorId);
        return AospPropertyManagerHelper.isCarPropertyActive(propId, AospPropertyMapping.DEFAULT_ZONE);
    }

    @Override
    public int getSensorEvent(int sensorId) {
        final CarPropertyValue<Integer> value = AospPropertyManagerHelper.getCarPropertyValue(AdaptWrapperHelper.sensorIdToPropertyId(sensorId), AospPropertyMapping.DEFAULT_ZONE);
        if (value == null) {
            return IllegalData.FunctionIllegalValue.NOT_AVAILABLE_RTN_VALUE;
        }
        Integer event = value.getValue();
        return event == null ? IllegalData.SensorIllegalValue.SENSOR_EVENT_UNKNOWN : AdaptWrapperHelper.propertyValueToSensorValue(sensorId, value.getValue());
    }

    @Override
    public float getSensorFloatValue(int sensorId) {
        final CarPropertyValue<Float> value = AospPropertyManagerHelper.getCarPropertyValue(AdaptWrapperHelper.sensorIdToPropertyId(sensorId), AospPropertyMapping.DEFAULT_ZONE);
        if (value == null) {
            return IllegalData.FunctionIllegalValue.NOT_AVAILABLE_RTN_VALUE;
        }
        Float floatValue = value.getValue();
        return floatValue == null ? IllegalData.SensorIllegalValue.SENSOR_NULL_FLOAT : floatValue;
    }

    @Override
    public boolean registerListener(ISensorListener listener, int sensorId) {
        if (listener == null) {
            return false;
        }
        final int listenerHashCode = listener.hashCode();
        PropertyEventCallbackWrapper wrapper = mEventCallbackWrapperMap.get(listenerHashCode);
        if (wrapper == null) {
            wrapper = new PropertyEventCallbackWrapper(listener);
        }
        int propId = AdaptWrapperHelper.sensorIdToPropertyId(sensorId);
        if (!AospPropertyManagerHelper.isCarPropertySupported(propId)) {
            LogUtils.w(TAG, "registerListener sensorId:" + sensorId + ", but not supported");
            listener.onSensorSupportedChanged(sensorId, FunctionStatus.STATUS_NOT_AVAILABLE);
        } else if (!isSensorActive(sensorId)) {
            LogUtils.w(TAG, "registerListener sensorId:" + sensorId + ", but not active");
            listener.onSensorSupportedChanged(sensorId, FunctionStatus.STATUS_NOT_ACTIVE);
        } else {
            boolean result = AospPropertyManagerHelper.registerPropertyEventCallback(wrapper, propId);
            if (result) {
                wrapper.addRegisteredId(sensorId, propId);
                LogUtils.d(TAG, "registerListener success, sensorId:" + sensorId);
            } else {
                LogUtils.w(TAG, "registerListener failed, sensorId:" + sensorId);
            }
        }
        mEventCallbackWrapperMap.put(listenerHashCode, wrapper);
        return true;
    }

    @Override
    public boolean unregisterListener(ISensorListener listener) {
        if (listener == null) {
            return false;
        }
        final PropertyEventCallbackWrapper wrapper = mEventCallbackWrapperMap.remove(listener.hashCode());
        if (wrapper == null) {
            LogUtils.w(TAG, "unregisterListener listener:" + listener + ", but not registered.");
            return false;
        }
        boolean result = AospPropertyManagerHelper.unregisterPropertyEventCallback(wrapper);
        if (result) {
            wrapper.clear();
        }
        LogUtils.d(TAG, "unregisterListener success, listener:" + listener);
        return result;
    }

}
